$(function() {
    query();
    $("#qtn").addClass("m_active");
    $(".foot").addClass("bg_grey");
    if ($("#flag").val()) {
        setDelTip();
    }
    // Change bg in the create page
    $(".a_radio").change(function() {
        $(".answer_input").css("background", "#ffffff");
        $(this).next().css("background", "#d2dae3");
    });
    $(".a_radio:checked").next().css("background", "#d2dae3");

    $(".close").click(function() {
        $("#dialog").hide();
        $("#del_con").hide();
        $(".page_disabled").hide();
    });

    $("#all_del").click(function() {
        $("input[name='del_one']").prop("checked", $(this).prop("checked"));
    });

    $("#delete").click(function() {
        if ($("input[name='del_one']:checked").length != 0) {
            $("#del_con").show();
            $(".page_disabled").show();
        } else {
            getTip("No item is chosen!");
        }
    });
    $("#del_yes").click(function() {
        var param = [];
        $("input[name='del_one']:checked").each(function() {
            param.push($(this).val());
        });
        window.location = "question/batch?id=" + param;
    });
})
// Pagination AJAX
function differList(x) {
    $(".question_table tr:gt(0)").remove();
    if (x.length > 0) {
        $.each(x, function(index, item) {
            $(".question_table").append(
                    "<tr class='table_body'>" + "<td class='q_no'>" + (index + 1) + "</td> " + "<td class='q_id'>"
                    + "Question" + item.id + "</td> " + "<td title='"+item.questionContent+"' class='q_des'><a href='question/queryId?id=" + item.id
                    + "'><xmp>" + item.questionContent + "</xmp></a></td> "
                    + "<td class='q_edit'><a href='question/updatePage?id=" + item.id
                    + "'><img src='/oes/static/images/ICN_Edit_15x15.png'></a></td>"
                    + "<td class='q_select'><input onclick='delOne()' class='delete' name='del_one' type='checkbox' value='" + item.id
                    + "' /></td>" + "</tr>");
        });
    } else {
        $(".question_table").append("<tr class='table_body'><td align='center' colspan='5'>No Data!</td></tr>");
    }
}
function query() {
    $.ajax({
        type : "get",
        datatype : "json",
        data : $("#pageInfo").serialize(),
        url : "question/queryJson",
//        beforeSend : function() {
//            
//        },
        success : function(msg) {
            var jsonObj = eval("(" + msg + ")");
            differList(jsonObj.itemList);
            setInfo(jsonObj.pageSize, jsonObj.fuzzyWord, jsonObj.currentPage, jsonObj.pageCount);
            getPageBar(jsonObj.currentPage, jsonObj.pageCount)
            $("#all_del").prop("checked", false);
        },
//        complete : function() {
//            
//        },
        error : function() {
            alert("Load Failed!");
        }
    });
}
function delOne() {
    var isAllChecked = true;
    $("input[name='del_one']").each(function() {
        if (!$(this).prop("checked")) {
            isAllChecked = false;
        }
    });
    $("#all_del").prop("checked", isAllChecked);
}
function setDelTip() {
    var value = $("#flag").val();
    if(value == 2) {
        getTip("Delete Successfully!");
    } else if (value == 3) {
        getTip("Edit Successfully!");
    }
}
